define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();

            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: 'card/appropriate/entity',
                    toolbar: '#toolbar1',
                    sortName: 'id',
                    fixedColumns: true,
                    fixedRightNumber: 1,
                    visible: false,
                    showToggle: false,
                    showColumns: false,
                    search:false,
                    showExport: false,
                    escape:false,
                    columns: [
                        [
                            {field: 'state', checkbox: true, },
                            {field: 'logtype', title: __('Logtype'), operate: false},
                            {field: 'entitysite.kabaw_name', title: __('Kabaw_name'), operate: 'LIKE'},
                            {field: 'number', title: __('Number'), operate: false},
                            {field: 'residue', title: __('Residue'), operate: false},
                            {field: 'number_from', title: __('Number_from'), operate: false},
                            {field: 'number_to', title: __('Number_to'), operate: false},
                            {field: 'entitysite.kabaw_fate', title: __('Kabaw_fate'), operate: false},
                            {field: 'buttons', title: __('Operate'), table: table1,
                                buttons: [
                                    {
                                        name: 'transfer',
                                        title: __('划拨'),
                                        classname: 'btn btn-xs btn-success btn-dialog',
                                        text: '划拨',
                                        //icon: 'fa fa-folder-o',
                                        url: function(row,column){
                                            var url='card/appropriate/transfer?id='+row.id;
                                            return url;
                                        },
                                        visible:function (row) {
                                            if(row.residue<=0){
                                                return false;
                                            }
                                            return true;
                                        }
                                    },
                                ],
                                formatter: Table.api.formatter.buttons, operate: false
                            },
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {
                // 表格2
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: 'card/transferlog',
                    extend: {
                        index_url: 'card/transferlog/index',
                        add_url: '',
                        edit_url: '',
                        del_url: '',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    visible: false,
                    showToggle: false,
                    showColumns: false,
                    search:false,
                    showExport: false,
                    columns: [
                        [
                            {field: 'id', title: __('Id'), operate:false},
                            {field: 'card_entity.kabaw_name', title: __('Kabaw_name'), operate:false},
                            {field: 'transfer_num', title: __('Transfer_num'), operate:false},
                            {field: 'transfer_data', title: __('Transfer_data'), searchList: {"0":__('Transfer_data 0'),"1":__('Transfer_data 1'),"2":__('Transfer_data 2')}, formatter: Table.api.formatter.normal},
                            //{field: 'card_dealer_id', title: __('Card_dealer_id')},
                            {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                            {field: 'dealer.name', title: __('Card_dealer_id'), operate: 'LIKE'},
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
            },
            three: function () {
                // 表格2
                var table3 = $("#table3");
                table3.bootstrapTable({
                    url: 'card/entitylog/exchange',
                    extend: {
                        index_url: 'card/entitylog/exchange',
                        add_url: '',
                        edit_url: '',
                        del_url: '',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar3',
                    sortName: 'id',
                    visible: false,
                    showToggle: false,
                    showColumns: false,
                    search:false,
                    showExport: false,
                    columns: [
                        [
                            {field: 'card_entity_id', title: __('Card_entity_id')},
                            {field: 'id', title: __('Id'), operate:false},
                            {field: 'site.kabaw_name', title: __('Site.kabaw_name'), operate: 'LIKE'},
                            {field: 'member.nickname', title: __('Member.nickname'), operate: 'LIKE'},
                            {field: 'exchange_time', title: __('Exchange_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                            {field: 'dealer.name', title: __('Dealer.name'), operate: 'LIKE'},
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table3);
            },
            five: function () {
                // 表格2
                var table5 = $("#table5");
                table5.bootstrapTable({
                    url: 'card/entitylog/logList',
                    extend: {
                        index_url: 'card/entitylog/logList',
                        add_url: '',
                        edit_url: '',
                        del_url: '',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar5',
                    sortName: 'id',
                    sortOrder:'desc',
                    fixedColumns: true,
                    fixedRightNumber: 1,
                    visible: false,
                    showToggle: false,
                    showColumns: false,
                    search:false,
                    showExport: false,
                    escape:false,
                    columns: [
                        [
                            {field: 'id', title: __('Id'), operate:false},
                            {field: 'transfer_data', title: __('Transfer_data'), searchList: {"0":__('Transfer_data 0'),"1":__('Transfer_data 1'),"2":__('Transfer_data 21'),"3":__('Transfer_data 31')}, formatter: Table.api.formatter.normal},
                            {field: 'site.kabaw_name', title: __('Logtype'), operate: false},
                            {field: 'site.kabaw_name', title: __('Kabaw_name'), operate: 'LIKE'},
                            {field: 'exchange_number', title: __('Exchange_number'), operate:false},
                            {field: 'status', title: __('Status'), searchList: {"0":__('待激活'),"1":__('已激活'),"2":__('已失效')}, formatter: Table.api.formatter.status},
                            {field: 'dealer.name', title: __('Card_dealer_id'), operate: 'LIKE'},
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table5);
            },
            four: function () {
                // 表格2
                var table4 = $("#table4");
                table4.bootstrapTable({
                    url: 'card/entitylog/index',
                    extend: {
                        index_url: 'card/entitylog/index',
                        add_url: '',
                        edit_url: '',
                        del_url: '',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar4',
                    sortName: 'id',
                    sortOrder:'asc',
                    fixedColumns: true,
                    fixedRightNumber: 1,
                    visible: false,
                    showToggle: false,
                    showColumns: false,
                    search:false,
                    showExport: false,
                    escape:false,
                    columns: [
                        [
                            {field: 'id', title: __('Id'), operate:false},
                            {field: 'transfer_data', title: __('Transfer_data'), searchList: {"0":__('Transfer_data 0'),"1":__('Transfer_data 1'),"2":__('Transfer_data 21'),"3":__('Transfer_data 31')}, formatter: Table.api.formatter.normal},
                            {field: 'logtype', title: __('Logtype'), operate: false},
                            {field: 'site.kabaw_name', title: __('Kabaw_name'), operate: 'LIKE'},
                            {field: 'exchange_number', title: __('Exchange_number'), operate:false},
                            {field: 'dealer.name', title: __('Card_dealer_id'), operate: 'LIKE'},
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table4);
            },
        },
        add: function () {
            Controller.api.bindevent();
        },
        transfer: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
